import { Action, ActionKey } from "@/action/Action";
import { localFile } from "@/localFile/LocalFile";
import { DiagramTableEditor } from "../../comps/station/graphTabs/diagramTableNew/DiagramTableEditor";
import { modelService } from "../../service";

export class TableExportReqifAction extends Action {
  desc="导出Excel"
  keyName = ActionKey.TableExportReqif

  async onTableToolbarClick(tableEditor: DiagramTableEditor) {

    const res = await localFile.chooseSaveFilePath({
      title: "选择保存的位置",
      filters: [
        {
          name: 'Reqif Files',
          extensions: ['reqif']
        }
      ],
      defaultPath: tableEditor.tab.diagramName
    });
    const filePath = res.filePath;
    if (res.canceled || !filePath) return;
    const result = await modelService.tableExportReqif(app.activeProject!.id, tableEditor.modelId);
    localFile.writeFile(filePath, result)
      .then(() => app.$notify({
        type: 'success',
        title: '成功',
        message: '导出成功'
      }))
      .catch (() => app.$notify({
        type: 'error',
        title: "失败",
        message: '资源繁忙或者被锁定'
      }));

  }
}